package com.example.demo.web.controller;
import java.util.Date;
import java.util.HashMap;
import javax.servlet.http.HttpServletResponse;
import com.example.demo.api.AipImageClient;
import org.json.JSONObject;
import com.example.demo.bean.CallLog;
import com.example.demo.service.ICallLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.baidu.aip.imageclassify.AipImageClassify;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(description = "图像识别相关接口")
@Validated
@RestController
@RequestMapping("/recognitioncar")
public class recognitioncarController {
    @Autowired
    private ICallLogService callLogService;
    @ApiOperation(value = "车辆识别")
    @PostMapping("car")
    public void sample(@RequestParam("file") MultipartFile file,HttpServletResponse resp) throws Exception {
        // 获取图像识别客户端
        AipImageClassify client = AipImageClient.getInstance();
        HashMap<String, String> options = new HashMap<>();
        options.put("baike_num" , "5");
        JSONObject res = client.carDetect(file.getBytes(), options);
        //  调用sdk完成识别功能
        //  手动返回json
        //  定义相应报文相应头信息
        resp.setHeader("Content-Type", "application/json;charset=UTF-8");
        CallLog log=new CallLog();
        log.setCallTime(new Date().getTime());
        log.setComment(res.toString());
        callLogService.save(log);
        //	将识别结果写入到相应体
        resp.getWriter().write(res.toString());
    }
}

